package com.atguigu.common.utils;

import java.util.Collection;
import java.util.Map;

/**
 * 对象工具类
 *
 * @author lax
 */
public class DataUtil {

    /**
     * 判断元素不为空
     *
     * @param obj object
     * @return boolean
     */
    public static boolean isNotEmpty(Object obj) {
        return !DataUtil.isEmpty(obj);
    }

    /**
     * 判断元素为空
     *
     * @param pObj
     * @return boolean
     */
    public static boolean isEmpty(Object pObj) {
        if (pObj == null)
            return true;
        if (pObj == "")
            return true;
        if (pObj instanceof String) {
            if (((String) pObj).trim().length() == 0) {
                return true;
            }
        } else if (pObj instanceof Collection<?>) {
            if (((Collection<?>) pObj).size() == 0) {
                return true;
            }
        } else if (pObj instanceof Map<?, ?>) {
            if (((Map<?, ?>) pObj).size() == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 循环判断元素为空
     *
     * @param pObj
     * @return
     */
    public static boolean isAnyEmpty(Object... pObj) {
        for (int i = 0; i < pObj.length; i++) {
            if (isEmpty(pObj[i])) {
                return true;
            }
        }
        return false;
    }


    public static String getArrayIndexData(String[] line, int index) {
        if (line.length < (index + 1)) {
            return null;
        }
        return line[index].trim();
    }

    public static Object getNullDefaultData(Object obj, Object defaultData) {
        return isEmpty(obj) ? defaultData : obj;
    }

}
